﻿#region Creditos

/* Autor: Edilberto Sánchez Forero	*/
/* Email: esanchezfo@gmail.com		*/
/* Licencia: Propietario			*/
/* País: Colombia, Bogotá			*/

#endregion

#region Librerias

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ESF.General.MVP;
using ESF.SSO.Core.BusinessLogic;
using ESF.General.Entity;
using ESF.SSO.Core.Entities;

#endregion


namespace ESF.SSO.Presenter.Admin.BasicReference
{
	public class EditBasicReferencePresenter : BaseEditPresenter<IEditBasicReferenceViewer, LocatedBasicReference>
	{
		#region Constructor
		
		public EditBasicReferencePresenter (IEditBasicReferenceViewer pViewer) : base (pViewer) {}
		
		#endregion
		
		
		#region Funciones

		public override LocatedBasicReference Get()
		{
			return CtrlBasicReference.Get (
				new LocatedBasicReference () { ID = _Viewer.ItemID });
		}


		public override void PrepareFields()
		{
			_Viewer.frmTitle = "Editar " + CtrlBasicReference.GetBasicReferenceType (new BasicReferenceType () { ID = _Viewer.BasicReferenceTypeID }).Name;
		}


		public override void Load (LocatedBasicReference pBasRef)
		{
			_Viewer.Code = pBasRef.Code;
			_Viewer.Name = pBasRef.Name;
			_Viewer.Description = pBasRef.Description;
			_Viewer.Extra_I = pBasRef.Extra_I;
			_Viewer.Extra_II = pBasRef.Extra_II;
			_Viewer.Extra_III = pBasRef.Extra_III;
			_Viewer.BasicReferenceTypeID = pBasRef.BasicReferenceType.ID;
			_Viewer.IsEnabled = pBasRef.IsEnabled.Value;
			
			_Viewer.CreationDateTime = pBasRef.CreationDateTime;
			_Viewer.LastUpdateDateTime = pBasRef.LastUpdateDateTime;
			_Viewer.UserLastUpdateAlias = pBasRef.UserLastUpdateAlias;
			_Viewer.UserCreationAlias = pBasRef.UserCreationAlias;
			
			_Viewer.Location = pBasRef.Location;

			_Viewer.TreeBasicReferenceTypeList = CtrlBasicReference.ListTypes ();
			_Viewer.ParentTreeBasicReferenceList = null;
		}


		public override void Clear()
		{
			base.Clear ();

			_Viewer.Code = string.Empty;
			_Viewer.Name = string.Empty;
			_Viewer.Description = string.Empty;
			_Viewer.Extra_I = string.Empty;
			_Viewer.Extra_II = string.Empty;
			_Viewer.Extra_III = string.Empty;
			
			_Viewer.UserCreationAlias = string.Empty;
			_Viewer.UserLastUpdateAlias = string.Empty;
			_Viewer.CreationDateTime = null;
			_Viewer.LastUpdateDateTime = null;
			
			_Viewer.IsEnabled = false;
			
			_Viewer.Location = null;
		}
		
		
		public override void Save ()
		{
			LocatedBasicReference basRef;
			
		
			basRef = new LocatedBasicReference ()
			{
				ID = _Viewer.ItemID,
				Code = _Viewer.Code,
				Name = _Viewer.Name,
				BasicReferenceType = new BasicReferenceType() { ID = _Viewer.BasicReferenceTypeID },
				Description = _Viewer.Description,
				Extra_I = _Viewer.Extra_I,
				Extra_II = _Viewer.Extra_II,
				Extra_III = _Viewer.Extra_III,
				IsEnabled = _Viewer.IsEnabled,
				
				CreationDateTime = DateTime.Now,
				UserCreationAlias = _Viewer.frmUserAlias,
				LastUpdateDateTime = DateTime.Now,
				UserLastUpdateAlias = _Viewer.frmUserAlias,
				
				Location = _Viewer.Location
			};
		
			try
			{
				basRef.ID = CtrlBasicReference.Register (basRef);
				
				_Viewer.ItemID = basRef.ID;
				
				SetSavedMsg ();
			}
			catch (Exception pEx)
			{
				LogError (pEx);
			}
		}


		public void AssociateTreeParent ()
		{
			try
			{
				CtrlBasicReference.RegisterByIDs (
					new TreeBasicReference ()
					{
						Child = new ESF.General.Entity.BasicReference () { ID = _Viewer.ItemID },
						Parent = _Viewer.ParentTreeBasicReferenceSelected
					}); 

				_Viewer.frmInfoMsg = "La referencia ha sido asociada";
			}
			catch (Exception pEx)
			{
				LogError (pEx);
			}
		}


		public void DESAssociateTreeParent ()
		{
			if (_Viewer.TreeBasicReferenceTypeSelected == null)
			{
				_Viewer.frmInfoMsg = "Seleccione la referencia de la Categoria como Padre";		
				return;
			}

			try
			{
				CtrlBasicReference.UnRegisterByIDs (
					new TreeBasicReference ()
					{
						Child = new ESF.General.Entity.BasicReference () { ID = _Viewer.ItemID },
						Parent = _Viewer.ParentTreeBasicReferenceSelected
					}); 

				_Viewer.frmInfoMsg = "La referencia ha sido DES asociada";

				_Viewer.ParentTreeBasicReferenceSelected = null;
			}
			catch (Exception pEx)
			{
				LogError (pEx);
			}
		}


		public void LoadListParentTreeBasicReference ()
		{
			if (_Viewer.TreeBasicReferenceTypeSelected == null)
			{
				_Viewer.ParentTreeBasicReferenceList = null;
			}
			else
			{
				_Viewer.ParentTreeBasicReferenceList = CtrlBasicReference.List (_Viewer.TreeBasicReferenceTypeSelected);

				_Viewer.ParentTreeBasicReferenceSelected = 
					CtrlBasicReference.GetParentTreeBasicReference (
						new ESF.General.Entity.BasicReference () { ID = _Viewer.ItemID },
						_Viewer.TreeBasicReferenceTypeSelected);
			}
		}
		
		#endregion
	}
}
